home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / dec92.zip / WUTH.C < prev   
C/C++ Source or Header  |  1992-08-10  |  2KB  |  59 lines

  1. /* test stdio functions, part 2 */
  2. #include <assert.h>
  3. #include <errno.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6.  
  7. int main()
  8.     {    /* test basic workings of stdio functions */
  9.     char buf[32], tname[L_tmpnam], *tn;
  10.     FILE *pf;
  11.     static int macs[] = {
  12.         _IOFBF, _IOLBF, _IONBF, BUFSIZ, EOF, FILENAME_MAX,
  13.         FOPEN_MAX, TMP_MAX, SEEK_CUR, SEEK_END, SEEK_SET};
  14.  
  15.     assert(256 <= BUFSIZ && EOF < 0);
  16.     assert(8 <= FOPEN_MAX && 25 <= TMP_MAX);
  17.     assert(tmpnam(tname) == tname && strlen(tname) < L_tmpnam);
  18.     assert((tn = tmpnam(NULL)) != NULL
  19.         && strcmp(tn, tname) != 0);
  20.     pf = fopen(tname, "w");
  21.     assert(pf != NULL
  22.         && pf != stdin && pf != stdout && pf != stderr);
  23.     assert(feof(pf) == 0 && ferror(pf) == 0);
  24.     assert(fgetc(pf) == EOF
  25.         && feof(pf) == 0 && ferror(pf) != 0);
  26.     clearerr(pf);
  27.     assert(ferror(pf) == 0);
  28.     assert(fputc('a', pf) == 'a' && putc('b', pf) == 'b');
  29.     assert(0 <= fputs("cde\n", pf));
  30.     assert(0 <= fputs("fghij\n", pf));
  31.     assert(fflush(pf) == 0);
  32.     assert(fwrite("klmnopq\n", 2, 4, pf) == 4);
  33.     assert(fclose(pf) == 0);
  34.     assert(freopen(tname, "r", stdin) == stdin);
  35.     assert(fgetc(stdin) == 'a' && getc(stdin) == 'b');
  36.     assert(getchar() == 'c');
  37.     assert(fgets(buf, sizeof (buf), stdin) == buf
  38.         && strcmp(buf, "de\n") == 0);
  39.     assert(ungetc('x', stdin) == 'x');
  40.     assert(gets(buf) == buf && strcmp(buf, "xfghij") == 0);
  41.     assert(fread(buf, 2, 4, stdin) == 4
  42.         && strncmp(buf, "klmnopq\n", 8) == 0);
  43.     assert(getchar() == EOF && feof(stdin) != 0);
  44.  
  45.     assert(fclose(stdin) == 0);    /* added */
  46.  
  47.     remove(tn);
  48.     assert(rename(tname, tn) == 0
  49.         && fopen(tname, "r") == NULL);
  50.     assert((pf = fopen(tn, "r")) != NULL && fclose(pf) == 0);
  51.     assert(remove(tn) == 0 && fopen(tn, "r") == NULL);
  52.     assert((pf = tmpfile()) != NULL && fputc('x', pf) == 'x');
  53.     errno = EDOM;
  54.     perror("Domain error reported as");
  55.     putchar('S'), puts("UCCESS testing <stdio.h>, part 2");
  56.     return (0);
  57.     }
  58.  
  59.